package com.shuai.api_platform.utils;

import cn.hutool.core.io.FileUtil;
import com.shuai.api_platform.common.ErrorCode;
import com.shuai.api_platform.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;


@Slf4j

public class FileUtils {
    /**
     * 返回存储文件名
     *
     * @param file 文件
     * @return 存储的文件名
     */
    public static String getStorageName(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isBlank(originalFilename)
                || !originalFilename.contains(".")) {
            LogUtils.error("上传文件格式错误！");
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件格式错误");
        }
        String suffix = "." + FileUtil.getSuffix(originalFilename);
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        return name + suffix;
    }
}
